new Vue({
    el: '#app',
    data() {
        return {
            form: {
                page: 1,
                limit: 10,
                Article_Name: '',
                Selected:false,
                Enterprise_Info_SerialNumber:$.cookie('enterprise')
            },
            tableData: [],
            total: 0,
            multipleSelection: [],//多选中的商品
        }
    },
    mounted() {
        this.getList(this.form)
    },
    methods: {
        getList(data) {
            const that = this;
            $.ajax({
                type: 'POST',
                data,
                url: DomainName + '/getSelectArticlePoolAction?token=' + $.cookie("token"),
                success: function (res) {
                    if (res.Status == "success") {
                        that.tableData = res.tableData
                        that.total = parseInt(res.limit);
                    }
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        onSerach() {
            this.form.page = 1;
            this.getList(this.form)
        },
        handleSizeChange(val) {//页码
            this.form.page = 1;
            this.form.limit = val;
            this.getList(this.form)
        },
        handleCurrentChange(val) {//查询条数
            this.form.page = val;
            this.getList(this.form)
        },
        handleSelectionChange(val) {//多选
            this.multipleSelection = val;
        },
        //配置项
        selectable(row) {
            return row.Selected_SerialNumber == ''
        },
        handleBatchAdd() {//批量添加
            if (this.multipleSelection.length > 0) {
                this.$confirm('是否将勾选的商品加入到商品池中?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'info'
                }).then(() => {
                    let selected_article_pool = [];
                    this.multipleSelection.forEach(row => {
                        selected_article_pool.push({
                            Selected_SerialNumber: '',
                            Article_SerialNumber: row.Article_SerialNumber,
                            Dealer_Unit_Price_Detail_SerialNumber: row.Dealer_Unit_Price_Detail_SerialNumber,
                            Enterprise_Info_SerialNumber: $.cookie('enterprise')
                        })
                    })
                    this.Add({ Data: JSON.stringify({ selected_article_pool }) });
                }).catch(() => { });
            } else {
                this.$message({
                    type: 'warning',
                    message: '至少要选择1个商品!'
                });
            }
        },
        handleAdd(row) {//选品
            if(row.Selected_SerialNumber==''){
                this.$confirm('是否将该商品加入到商品池中?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'info'
                }).then(() => {
                    this.Add({
                        Data: JSON.stringify({
                            selected_article_pool: [{
                                Selected_SerialNumber: '',
                                Article_SerialNumber: row.Article_SerialNumber,
                                Dealer_Unit_Price_Detail_SerialNumber: row.Dealer_Unit_Price_Detail_SerialNumber,
                                Enterprise_Info_SerialNumber: $.cookie('enterprise')
                            }]
                        })
                    });
                }).catch(() => { });
            }else{
                this.$confirm('是否将该商品移除已选商品池?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'info'
                }).then(() => {
                    $.ajax({
                        type: 'POST',
                        data:{
                            TableName:'selected_article_pool',
                            Primary_Key:row.Selected_SerialNumber,
                            Primary_Key_Name:'Selected_SerialNumber',
                        },
                        url: DomainName + '/DeleteAction?token=' + $.cookie("token"),
                        success: (res)=> {
                            if (res.Status == 'success') {
                                this.$message({
                                    type: 'success',
                                    message: '取消成功!'
                                });
                                this.getList(this.form);
                            } else {
                                this.$message({
                                    type: 'error',
                                    message: '提交失败，请重试!'
                                });
                            }
                        },
                        complete: function (XMLHttpRequest) {
                            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                            }
                        },
                        dataType: "json"
                    });
                }).catch(() => { });
            }
        },
        Add(data) {//添加
            const that = this;
            $.ajax({
                type: 'POST',
                data,
                url: DomainName + '/getAddAction?token=' + $.cookie("token"),
                success: function (res) {
                    console.log(res);
                    if (res.Status == 'success') {
                        that.$message({
                            type: 'success',
                            message: '选品成功!'
                        });
                        that.getList(that.form);
                    } else {
                        that.$message({
                            type: 'error',
                            message: '提交失败，请重试!'
                        });
                    }
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        }
    }
})